#!/usr/bin/perl -w

####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################

#===================================================================================================
# BigDaddy CronTab Monitor
# Monitor Delayed Crons and "Enable" them if the time for the delay is gone.
# Dups 24.03.2004
#===================================================================================================

BEGIN
{
	push(@INC,'/bigdaddy')
}

use strict;
use Config::IniFiles;
use BioMain qw(:allfunctions);
use DBI;




#Before we do anything we are going to bring up the main ini file: bigdaddy.ini should be in the
#same dir as these executables.
my $bigdaddy_cfg= new Config::IniFiles( -file => "/bigdaddy/bigdaddy.ini" );

#Get the dbini handle
my $dbIniHandle = $bigdaddy_cfg->val('main', 'dbinihandle');

#is this cron supposed to be run from this server?
my $cronMaster = $bigdaddy_cfg->val('main', 'cronmaster');

if ($cronMaster ne 'true')
{
	die("This Server is not the BigDaddy Cron Master");
}

my $cronID=9;
&cronstart($cronID);

my $dbb = callfor_db_handle($dbIniHandle, 1, 99);

my $crontabIni="crontab";

my $sql="SELECT inifile, ID FROM inifile WHERE ID IN ('".$crontabIni."')";
my $result=$dbb->prepare( $sql );
$result->execute;

while (my $ref=$result->fetchrow_hashref())
{
    open (TMPINIFILE, ">/tmp/".$ref->{'ID'}.".ini") or die "Could not open temp file for writing: $!";
    print TMPINIFILE $ref->{'inifile'};
    close (TMPINIFILE) or die "Could not close temp file: $!";
}

my $cfg_crontab= new Config::IniFiles( -file => "/tmp/".$crontabIni.".ini" );

# Check for the crontab delay value
my $delay=$cfg_crontab->val('global','globaldelay');
my $status=$cfg_crontab->val('global','globalstatus');

# if the status is currently set to delay and the delay time is less than the current
# time then set the delay to 0, enable it and put the last_ranenabled to the current time.
if ($status eq 'delay' && $delay<time())
{
    $cfg_crontab->setval('global','globaldelay', '0');
    $cfg_crontab->setval('global','globalstatus', 'on');
    $cfg_crontab->WriteConfig("/tmp/".$crontabIni.".ini");

    open (TMPINIFILE, "</tmp/".$crontabIni.".ini") or die "Could not open temp file for reading: $!";
    undef $/;
    my $updateIni=<TMPINIFILE>;
    close (TMPINIFILE);

    $sql="UPDATE inifile SET inifile='".$updateIni."' WHERE ID='".$crontabIni."'";
    $dbb->do( $sql );

    $sql="UPDATE cronjob SET last_ranenabled=".time()." WHERE active=1";
    #$dbb->do( $sql );
}

# Now check for each individual cronjob
$sql="UPDATE cronjob SET active=1, delayed_until=0, last_ranenabled=".time()." WHERE active=2 AND delayed_until>0 AND delayed_until<".time();
$dbb->do( $sql );

&cronend($cronID);


